<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
	<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
	<meta HTTP-EQUIV="Expires" CONTENT="0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试报告自动导出</title>
<style type="text/css">
	@import "../dojo-release-1.5.0/dijit/themes/tundra/tundra.css";
	@import "../dojo-release-1.5.0/dojo/resources/dojo.css";
	@import "../dojo-release-1.5.0/dojox/grid/resources/tundraGrid.css";
</style>
<script type="text/javascript" src="../dojo-release-1.5.0/dojo/dojo.js" djConfig="parseOnLoad:true"></script>
<script type="text/javascript" src="../framework/uiutil.js"></script>
<script type="text/javascript" src="../framework/simpleCRUDUtil.js"></script>
<script type="text/javascript">
dojo.addOnLoad(function(){
	refresh();
});

function refresh(){
	dojo.xhrGet({
		 url:"../bkgrd/getTestReportAutoExportConfig.do",
		 handleAs:"json",
		 preventCache:true,
		 load:function(response){
			dojo.byId("savePath").value = response["savePath"];
			dojo.byId("intervalInHour").value = response["intervalInHour"];
			dijit.byId("lastExportTimestamp_date").set("value",new Date(response["lastExportTimestamp"]));
			dijit.byId("lastExportTimestamp_time").set("value",new Date(response["lastExportTimestamp"]));
			dijit.byId("startAutoExport").setChecked(response["startAutoExport"]!=0);
			if(response["exporting"]){	//如果正在导出，则不允许立即导出
				dijit.byId("saveCfg").set("disabled", true);
				dijit.byId("exportImmediately").set("disabled", true);
				dijit.byId("exportImmediately").set("label", "正在导出数据...");
			}else{
				dijit.byId("saveCfg").set("disabled", false);
				dijit.byId("exportImmediately").set("disabled", false);
				dijit.byId("exportImmediately").set("label", "立即导出");
			}

			if(response["uploading"]){	//如果正在上传，不允许立即上传
				dijit.byId("uploadFtp").set("disabled", true);
				dijit.byId("uploadFtp").set("label", "正在上传FTP...");
			}else{
				dijit.byId("uploadFtp").set("disabled", false);
				dijit.byId("uploadFtp").set("label", "上传FTP");
			}

			if(response["exporting"] || response["uploading"]){	//不论是正在上传还是正在导出，都不允许删除
				dijit.byId("deleteDir").set("disabled", true);
			}else{
				dijit.byId("deleteDir").set("disabled", false);
			}
			
			dijit.byId("uploadToFtp").setChecked(response["uploadToFtp"]!=0);
			dojo.byId("uploadFtpUrl").value = response["uploadFtpUrl"];
			dojo.byId("uploadFtpUser").value = response["uploadFtpUser"];
			dojo.byId("uploadFtpPassword").value = response["uploadFtpPassword"];
			autoUploadToFtpCheck();
		 }
	});

	new SimpleCrudUtil({
		r:"../bkgrd/getTestReportExportResultList.do",
		grid:"grid",
		idName:"forderName"
		}).R();
}

function saveCfg(){
	var savePath = dojo.byId("savePath").value;
	var intervalInHour =parseInt(dojo.byId("intervalInHour").value);
	var startAutoExport = D.getSingleCheckBoxCheckedInt("startAutoExport");
	var lastExportTimestamp = D.getWholeDateTime("lastExportTimestamp_date","lastExportTimestamp_time").getTime();

	var uploadToFtp = D.getSingleCheckBoxCheckedInt("uploadToFtp");
	var uploadFtpUrl = dojo.byId("uploadFtpUrl").value;
	var uploadFtpUser = dojo.byId("uploadFtpUser").value;
	var uploadFtpPassword = dojo.byId("uploadFtpPassword").value;
	
	var model = {savePath:savePath,intervalInHour:intervalInHour,lastExportTimestamp:lastExportTimestamp,
			startAutoExport:startAutoExport,uploadToFtp:uploadToFtp,uploadFtpUrl:uploadFtpUrl,
			uploadFtpUser:uploadFtpUser,uploadFtpPassword:uploadFtpPassword}
	new SimpleCrudUtil({
		u:"../bkgrd/saveTestReportAutoExportConfig.do"
		}).U(model);
}

function exportImmediately(){
	saveCfg();
	dojo.xhrGet({
		 url:"../bkgrd/startExportImmediately.do",
		 preventCache:true,
		 load:function(response){
			alert(response);
			refresh();
		 }
	});
}
//是否自动上传FTP的checkbox事件
function autoUploadToFtpCheck(){
	var uploadToFtp = D.getSingleCheckBoxCheckedInt("uploadToFtp");
	if(uploadToFtp){
		dijit.byId("uploadFtpUrl").set("readOnly", false);
		dijit.byId("uploadFtpUser").set("readOnly", false);
		dijit.byId("uploadFtpPassword").set("readOnly", false);
	}else{
		dijit.byId("uploadFtpUrl").set("readOnly", true);
		dijit.byId("uploadFtpUser").set("readOnly", true);
		dijit.byId("uploadFtpPassword").set("readOnly", true);
	}
}

function uploadFtp(){
	var item = D.getSelectedRowOfTable("grid")[0];
	if(item == null){
		alert("请至少选择表格中的一行来进行上传");
		return;
	}
	
	saveCfg();
	dojo.xhrGet({
		 url:"../bkgrd/uploadExportedDir.do",
		 content:{dir:item["forderName"]},
		 encoding : 'UTF-8',
		 handleAs:"text",
		 preventCache:true,
		 load:function(response){
			alert(response);
			refresh();
		 }
	});
	
}

function deleteDir(){
	new SimpleCrudUtil({
		d:"../bkgrd/deleteExportedDir.do",
		r:"../bkgrd/getTestReportExportResultList.do",
		grid:"grid",
		idName:"forderName"
		}).D();
}
</script>

<style type="text/css">
#grid{
	border:1px solid #333;
	width:80%;
	margin:10px;
	height:500px;
	font-size:0.9em;
}
</style>
</head>
<body class="tundra">
<table width="100%">
<tr><td>是否自动导出</td><td><input dojoType="dijit.form.CheckBox" id="startAutoExport"/></td>
<td>导出文件保存位置</td><td><input type="text" dojoType="dijit.form.TextBox" id="savePath" size="30" trim="true" value=""/></td></tr>
<tr><td>导出间隔（小时）</td><td><input type="text" dojoType="dijit.form.TextBox" id="intervalInHour" size="30" trim="true" value=""/></td>
<td>上次导出时间</td><td>
<input type="text" id="lastExportTimestamp_date"  dojoType="dijit.form.DateTextBox" required="true" onchange="lastExportTimestamp_time.focus()"/>
<input type="text" id="lastExportTimestamp_time"  dojoType="dijit.form.TimeTextBox" required="true"/>
</td></tr>
<tr><td>导出后自动上传FTP</td><td><input dojoType="dijit.form.CheckBox" id="uploadToFtp" onchange="autoUploadToFtpCheck()"/></td>
<td>FTP地址</td><td><input type="text" dojoType="dijit.form.TextBox" id="uploadFtpUrl" size="30" trim="true" value=""/></td></tr>
<tr><td>FTP用户名</td><td><input type="text" dojoType="dijit.form.TextBox" id="uploadFtpUser" size="30" trim="true" value=""/></td>
<td>FTP密码</td><td><input type="password" dojoType="dijit.form.TextBox" id="uploadFtpPassword" size="30" trim="true" value=""/></td></tr>


<tr><td colspan="4">

<button dojoType="dijit.form.Button" id="saveCfg" onclick="saveCfg()">保存参数</button>
<button dojoType="dijit.form.Button" id="exportImmediately" onclick="exportImmediately()">立即导出</button>
<button dojoType="dijit.form.Button" id="refesh" onclick="refresh()">刷新</button>
</td></tr>
</table>
<hr/>
<table width="80%"><tr><td>导出结果（只显示最近10条）：</td>
<td align="right"">
<button dojoType="dijit.form.Button" id="uploadFtp" onclick="uploadFtp()">上传FTP</button>
<button dojoType="dijit.form.Button" id="deleteDir" onclick="deleteDir()">删除</button>
</td></tr>
</table>
<table dojoType="dojox.grid.DataGrid" id="grid"  autoWidth="true">
 <thead>
	<tr>
	     <th field="forderName" width="350px">文件夹</th>
	     <th field="count" width="100px">报告数量</th>
	     <th field="path" width="500px">路径</th>
	</tr>
</thead>
</table>
</body>
</html>